package com.demo.dictprovider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;


public class DictProvider extends ContentProvider {

    private DataBaseHelper dbHelper;
    private UriMatcher matcher;

    @Override
    public boolean onCreate() {
        dbHelper = new DataBaseHelper(getContext(), "mydict.db");
        matcher = new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI(Words.AUTHORITY, "words", 1);
        matcher.addURI(Words.AUTHORITY, "word/#", 2);

        Log.v("my_debug", "dict provider is running...");
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        Log.v("my_debug", "Provider is querying...");
        switch (matcher.match(uri)) {
            case 1:
                cursor = dbHelper.getReadableDatabase().query("dict", projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case 2:
                long id = ContentUris.parseId(uri);
                String where = Words.Word._ID + "=" + id;
                if (selection != null && !selection.equals("")) {
                    where = where + " and " + selection;
                }
                cursor = dbHelper.getReadableDatabase().query("dict", projection, where, selectionArgs, null, null, sortOrder);
                break;
        }
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        switch (matcher.match(uri)) {
            case 1:
                return "vnd.android.cursor.dir/dict";
            case 2:
                return "vnd.android.cursor.item/dict";
            default:
                throw new IllegalArgumentException("非法Uri:" + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        long id = dbHelper.getReadableDatabase().insert("dict", null, values);
        Uri uri2 = ContentUris.withAppendedId(Words.Word.DICT_CONTENT_URI, id);
        return uri2;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int rows = -1;
        switch (matcher.match(uri)) {
            case 1:
                rows = dbHelper.getReadableDatabase().delete("dict", selection, selectionArgs);
                break;
            case 2:
                long id = ContentUris.parseId(uri);
                String where = Words.Word._ID + "=" + id;
                if (selection != null && !selection.equals("")) {
                    where = where + " and " + selection;
                }
                rows = dbHelper.getReadableDatabase().delete("dict", selection, selectionArgs);
                break;
        }
        return rows;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int rows = -1;
        switch (matcher.match(uri)) {
            case 1:
                rows = dbHelper.getReadableDatabase().update("dict", values, selection, selectionArgs);
                break;
            case 2:
                long id = ContentUris.parseId(uri);
                String where = Words.Word._ID + "=" + id;
                if (selection != null && !selection.equals("")) {
                    where = where + " and " + selection;
                }
                rows = dbHelper.getReadableDatabase().update("dict", values, selection, selectionArgs);
                break;
        }
        return rows;
    }
}
